#coding=utf-8
import json

# import xlwt

from django.http import HttpResponse


# Create your views here.
from apps.cm import export
# from tools import sqltoExcel


def cm_export_excel(request):

    if request.method == 'POST':
        form = request.POST
    elif request.method == 'GET':
        form = request.GET
    print '-----------------'
    return export.export_excel()

def cm_file_upload(request):

    if request.method == 'POST':
        form = request.POST
    elif request.method == 'GET':
        form = request.GET
    print '-----------------'
    return export.file_upload(request)

def cm_file_download(request):

    if request.method == 'POST':
        form = request.POST
    elif request.method == 'GET':
        form = request.GET
    # from django.core.mail import send_mail,EmailMessage
    #     # import os
    #     # BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
    #     # the_file_name = BASE_DIR + "/upload/"
    #     # attachname = the_file_name + "a.txt"
    #
    #
    # em=EmailMessage('subject','body','chengk@chinaetf.com.cn',to=['463241800@qq.com'])
    #     #em.attach_file(attachname)
    # em.send()
    return export.file_download(request)


def cm_send_email(request):

    if request.method == 'POST':
        form = request.POST
    elif request.method == 'GET':
        form = request.GET
    print '-----------------'
    return export.send_email(request)
    # if param == 'init':
    #     res = cmRiskWhite.init()
    #     return HttpResponse(json.dumps(res))
    #
    # elif param == 'save':
    #     invest_id = form.get('formobj.account_id')
    #     stock_code = form.get('formobj.stock_code')
    #     rule_id = form.get('formobj.rule_id')
    #
    #     if rule_id == '':
    #         cmRiskWhite.add(invest_id, stock_code)
    #     else :
    #         cmRiskWhite.modify(invest_id, stock_code, rule_id)
    #
    #     return HttpResponse(True)
    #
    # elif param == 'add':
    #     invest_id = form.get('formobj.account_id')
    #     stock_code = form.get('formobj.stock_code')
    #     rule_id = form.get('formobj.rule_id')
    #
    #     cmRiskWhite.add(invest_id, stock_code)
    #
    # elif param == 'delete':
    #     rule_id = form.get('rule_id')
    #     cmRiskWhite.delete(rule_id)
    #     return HttpResponse(True)
    #
    # elif param == 'modify':
    #     invest_id = form.get('invest_id')
    #     stock_code = form.get('stock_code')
    #     rule_id = form.get('rule_id')
    #     cmRiskWhite.modify(invest_id, stock_code, rule_id)
    #
    # elif param == 'query':
    #     invest_id = form.get('invest_id')
    #     stock_code = form.get('stock_code')
    #     rule_id = form.get('rule_id')
    #     res = cmRiskWhite.query(invest_id, stock_code, rule_id) # [(invest_id, 'stock_code', rule_id),(...)...]
    #
    #     # print invest_id, stock_code, res
    #
    #     rows = []
    #     for i in res:
    #         str_format = "{'rule_id':'%d', " \
    #                      "'invest_account_id':'%d', " \
    #                      "'invest_account_name':'%s'," \
    #                      "'stock_code':'%s'}"%(i[2],i[0],'name',i[1])
    #         rows.append(eval(str_format))
    #
    #     res = {'total': str(len(rows)), 'rows' : rows}
    #     return HttpResponse(json.dumps(res))

    # if param == 'export_excel':
    #     print '-------------------------------------'
    #     return export.export_excel()
    #
    # elif param == 'file_upload':
    #     return export.file_upload(request)


    # elif param == 'file_download':
    #     # do something...
    #     '''此处是使用django框架下的邮件系统进行email的发送'''
    #     from django.core.mail import send_mail,EmailMessage
        # import os
        # BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
        # the_file_name = BASE_DIR + "/upload/"
        # attachname = the_file_name + "a.txt"


        # em = EmailMessage('subject','body','chengk@chinaetf.com.cn',to=['463241800@qq.com'])
        # #em.attach_file(attachname)
        # em.send()
        # # send_mail('subject', 'this is the message of email', "zhengyb@chinaetf.com.cn", ['575393442@qq.com',], fail_silently=True)
        # return export.file_download(request)

    # elif param == '':
    #     #return render(request, 'rm/cmRiskWhite_bak.html')
    #     #return mymako.hello(request)
    #     #print request
    #     return httpUtil.mako_render(request,'/rm/hello.html',{'name':'mm'})
    #     #return mymako.mako_render(request,'/rm/cmRiskWhite_bak.html',{'name':'mm'})
    #     #return mymako.hello(request)

# def paradeal(request, model, action, method):
#     print 'dealed' , model, action , method
#
#     return HttpResponse('hello !')

# if __name__ == '__main__':
#     res = cmRiskWhite.query(2, '9090')
#     print res


